package com.sky.service.user.impl;

import cn.hutool.core.util.ObjectUtil;
import com.sky.constant.BaseServiceConstant;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.exception.ParameterInvalidException;
import com.sky.exception.ParameterIsNullException;
import com.sky.mapper.user.AddressBookMapper;
import com.sky.pojo.AddressBook;
import com.sky.service.user.AddressBookService;
import com.sky.utils.IdentityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author zgp
 * @version V1.0
 * @date 2025-01-27 18:31
 */
@Service
public class AddressBookServiceImpl implements AddressBookService {
    @Autowired
    private AddressBookMapper addressBookMapper;


    @Override
    public List<AddressBook> getAllAddressBook() {
        return addressBookMapper.selectAllByUserId(BaseContext.getCurrentId());
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void saveAddressBook(AddressBook addressBook) {
        // 参数校验
        if (ObjectUtil.isNotNull(addressBook)
                && (ObjectUtil.isNull(addressBook.getPhone())
                || ObjectUtil.isNull(addressBook.getDetail())
                || ObjectUtil.isNull(addressBook.getSex()))
        ) {
            throw new ParameterIsNullException(MessageConstant.PARAMES_IS_INVALID);
        }
        if(!IdentityUtil.validatePhone(addressBook.getPhone())){
            throw new ParameterInvalidException(MessageConstant.PHONE_IS_INVALID);
        }
        // 新增地址
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(BaseServiceConstant.ZERO);
        addressBookMapper.insert(addressBook);
    }

    @Override
    public AddressBook getDefaultAddress() {
        return addressBookMapper.selectDefaultAddressByUserId(BaseContext.getCurrentId());
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void setDefaultAddress(AddressBook addressBook) {
        if (ObjectUtil.isNull(addressBook) && ObjectUtil.isNull(addressBook.getId())) {
            throw new ParameterIsNullException(MessageConstant.ID_IS_NULL);
        }
        // 清除用户默认地址
        addressBookMapper.updateCleanDefaultAddressByUserId(BaseContext.getCurrentId());
        // 设置默认地址
        addressBookMapper.updateDefaultById(addressBook.getId());
    }

    @Override
    public AddressBook getAddressById(Long id) {
        if (ObjectUtil.isNull(id)) {
            throw new ParameterIsNullException(MessageConstant.ID_IS_NULL);
        }
        return addressBookMapper.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void deleteAddressById(Long id) {
        if (ObjectUtil.isNull(id)) {
            throw new ParameterIsNullException(MessageConstant.ID_IS_NULL);
        }
        addressBookMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void updateAddress(AddressBook addressBook) {
        // 参数校验
        if (ObjectUtil.isNotNull(addressBook)
                && (ObjectUtil.isNull(addressBook.getPhone())
                || ObjectUtil.isNull(addressBook.getDetail())
                || ObjectUtil.isNull(addressBook.getSex()))
        ) {
            throw new ParameterIsNullException(MessageConstant.PARAMES_IS_INVALID);
        }
        if(!IdentityUtil.validatePhone(addressBook.getPhone())){
            throw new ParameterInvalidException(MessageConstant.PHONE_IS_INVALID);
        }
        addressBookMapper.updateById(addressBook);
    }
}
